package com.qilin.hscs.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qilin.hscs.util.IpAddrUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 10188 on 2017/9/10.
 */
@Aspect
@Component
@Order(0)
public class HttpAspect {

    private static final Logger logger = LogManager.getLogger(HttpAspect.class.getName());

    @Pointcut("execution(public * com.qilin.hscs.controller.*.*(..))")
    public void log() {
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        //doIpInfoLogger(joinPoint);
        ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
       logger.info("host={}",request.getRemoteHost());
       logger.info("url={}",request.getRequestURL());
       logger.info("method={}",request.getMethod());
       logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
       logger.info("args={}",joinPoint.getArgs());
    }

   @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
        logger.info("response={}",object);
    }

    private void doIpInfoLogger(JoinPoint joinPoint) throws Exception {
        ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        String ipInfo= IpAddrUtil.getIpAddr(request.getRemoteAddr());
        JSONObject ipInfoObj=JSON.parseObject(ipInfo);
        String region=ipInfoObj.getString("region");
        String isp=ipInfoObj.getString("isp");
        String en_name=ipInfoObj.getString("en_name");
        String country=ipInfoObj.getString("country");
        String city=ipInfoObj.getString("city");
        int ret_code=ipInfoObj.getInteger("ret_code");
        String county=ipInfoObj.getString("county");
        String continents=ipInfoObj.getString("continents");
        String city_code=ipInfoObj.getString("city_code");
        String lnt=ipInfoObj.getString("lnt");
        String en_name_shor=ipInfoObj.getString("en_name_shor");
        String lat=ipInfoObj.getString("lat");
        logger.info("country={}",country);
        logger.info("region={}",region);
        logger.info("city={}",city);
        logger.info("county={}",county);
        logger.info("isp={}",isp);
        logger.info("en_name={}",en_name);
        logger.info("ret_code={}",ret_code);
        logger.info("continents={}",continents);
        logger.info("city_code={}",city_code);
        logger.info("lnt={}",lnt);
        logger.info("en_name_shor={}",en_name_shor);
        logger.info("lat={}",lat);
    }
}
